﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace RndSamples
{
    class Processor
    {
        Dictionary<string, List<string>> nesses = new Dictionary<string,List<string>>();
        
        int numFileProcessed = 0;
        public void ProcessFile(FileInfo fi) 
        {
            numFileProcessed++;
            Console.WriteLine("Processing " + fi.Name);
            TextReader tr_in = new StreamReader(fi.FullName);

            string strSource;
            strSource = tr_in.ReadToEnd();
            tr_in.Close();

            string pattern = @"(\.\.\.)|( )|(\.)|(,)|(!)|(\?)|(;)|(:)|(')|(\()|(\))";
            pattern += @"|(\"")";
            Regex RE = new Regex(pattern);

            //  Console.WriteLine("tokenizing: "+source_string);
            string[] parts = (RE.Split(strSource));

            
            for (int j = 0; j < parts.Length; j++)
            {
                if (parts[j].Replace(" ", "").Length != 0)
                {
                    string token = parts[j].ToLower();
                    if (token.Contains("ness")) // is ness
                    {
                        if (nesses.ContainsKey(token))
                        {
                            if (!nesses[token].Contains(fi.Name))
                            {
                                nesses[token].Add(fi.Name);
                            }
                        }
                        else
                        {
                            nesses.Add(token, new List<string>());
                            nesses[token].Add(fi.Name);
                        }
                    }
                    // tw_out.WriteLine(token);
                }
            }

          
        
            
            

        }
        public void ProcessNess(FileInfo fi)
        {
            TextWriter tw_out = new StreamWriter("./out/" + fi.Name);
            foreach (string token in nesses.Keys)
            {
                if ((nesses[token].Count < numFileProcessed) && (nesses[token].Contains(fi.Name)))
                    tw_out.WriteLine(token + "\t" + nesses[token].Count);
            }
            tw_out.Close();
        }

        public void ProcessTotal()
        {
            TextWriter tw_out = new StreamWriter("./total.txt");
            foreach (string token in nesses.Keys)
            {
                if (nesses[token].Count == numFileProcessed )
                    tw_out.WriteLine(token);
            }
            tw_out.Close();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, random samples");
            DirectoryInfo di = new DirectoryInfo("./in");
            FileInfo[] Files = di.GetFiles();
            try
            {
                if (Directory.Exists("./out")) Directory.Delete("./out", true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("./out :" + ex.Message);
            }
            try { Directory.CreateDirectory("./out"); }
            catch (Exception ex) { Console.WriteLine("./out :" + ex.Message); };

            Processor processor = new Processor();
            foreach (FileInfo fi in Files)
            {
                processor.ProcessFile(fi);
            }

            foreach (FileInfo fi in Files)
            {
                processor.ProcessNess(fi);
            }
            processor.ProcessTotal();



        }
    }
}
